package com.hongshan.wallet.core

import android.content.Intent
import android.net.Uri
import android.support.v7.app.AlertDialog
import android.webkit.JsResult
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.TextView


/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core
 * @author   : zhangxx
 * @date     : 2019/3/27
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
class MyWebChromeClient(
    val tvTitle: TextView, //    private ProgressBar progressBar;
    val act: WebActivity, private var uploadFiles: ValueCallback<Array<Uri>>?
) : WebChromeClient() {
    override fun onReceivedTitle(view: WebView, title: String) {
        if (!title.startsWith("http"))
            tvTitle.text = title
    }

    override fun onProgressChanged(view: WebView, newProgress: Int) {

    }

    override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
        val dialog = AlertDialog.Builder(act)
            .setMessage(message)
            .setCancelable(false)
            .setPositiveButton("确定", { d, v -> result.confirm() }).create()
        dialog.show()
        return true
    }

    override fun onShowFileChooser(
        webView: WebView,
        valueCallback: ValueCallback<Array<Uri>>,
        fileChooserParams: FileChooserParams
    ): Boolean {
        uploadFiles = valueCallback
        val i = Intent(Intent.ACTION_GET_CONTENT)
        i.addCategory(Intent.CATEGORY_OPENABLE)
        i.type = "*/*"
        act.startActivityForResult(Intent.createChooser(i, "test"), WebActivity.REQUEST_CODE_CHOOSE_FILE)
        return true
    }
}